<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>layui</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="../layuiadmin/layui/css/layui.css"
	media="all">
<!--  <link rel="stylesheet" href="../layuiadmin/style/admin.css" media="all">-->
<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>

<body>
<div class="demoTable">
  模糊检索：
  <div class="layui-inline">
    <input class="layui-input" name="id" id="demoReload" autocomplete="off">
  </div>
  <button class="layui-btn" data-type="reload">搜索</button>
</div>
	<!--<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">房间ID</label>
				<div class="layui-input-block">
					<input type="text" name="roomId" placeholder="请输入" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">房间编号</label>
				<div class="layui-input-block">
					<input type="text" name="roomNo" placeholder="请输入" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
			</div>
		</div>
		</div>-->
		<div id=editform style="display:none">
<form class="layui-form" action="" lay-filter="example" id="myeditform">
 
  <input type="hidden" name="attid" id="attid" >
   
  <div class="layui-form-item">
    <label class="layui-form-label">景点名字</label>
    <div class="layui-input-block">
      <input type="text" name="attname" id="attname" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">景点地址</label>
    <div class="layui-input-block">
      <input type="text" name="attaddress" id="attaddress" placeholder="请输入地址" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">门票价格</label>
    <div class="layui-input-block">
      <input type="text" name="attprice" id="attprice" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
    </div>
  </div>

  <!-- <div class="layui-form-item">
    <label class="layui-form-label">性别</label>
    <div class="layui-input-block">
      <input type="radio" name="userSex" value="男" title="男" checked="">
      <input type="radio" name="userSex" value="女" title="女">
    </div>
  </div> -->
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">景点描述</label>
    <div class="layui-input-block">
      <textarea placeholder="请输入景点描述" class="layui-textarea" name="attdescribe" id="attdescribe" value=""></textarea>
    </div>
  </div>
 
   <div class="layui-form-item">
    <div class="layui-input-block">
      <!-- <input type="button" id="formbtn" class="layui-btn" value="立即提交" > -->
      <button class="layui-btn " id="formbtn" lay-submit="" lay-filter="demo1" >立即提交</button>
    </div>
  </div>
</form>

</div>

<div id=editform2 style="display:none">
<form class="layui-form" action="" lay-filter="example" id="myeditform">
 
  <div class="layui-form-item">
    <label class="layui-form-label">景点名字</label>
    <div class="layui-input-block">
      <input type="text" name="attname" id="attname" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">景点地址</label>
    <div class="layui-input-block">
      <input type="text" name="attaddress" id="attaddress" placeholder="请输入地址" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">门票价格</label>
    <div class="layui-input-block">
      <input type="text" name="attprice" id="attprice" lay-verify="title" autocomplete="off" placeholder="请输入价格" class="layui-input">
    </div>
  </div>

  <!-- <div class="layui-form-item">
    <label class="layui-form-label">性别</label>
    <div class="layui-input-block">
      <input type="radio" name="userSex" value="男" title="男" checked="">
      <input type="radio" name="userSex" value="女" title="女">
    </div>
  </div> -->
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">景点描述</label>
    <div class="layui-input-block">
      <textarea placeholder="请输入景点描述" class="layui-textarea" name="attdescribe" id="attdescribe" value=""></textarea>
    </div>
  </div>
 
   <div class="layui-form-item">
    <div class="layui-input-block">
      <!-- <input type="button" id="formbtn" class="layui-btn" value="立即提交" > -->
      <button class="layui-btn " id="formbtn" lay-submit="" lay-filter="demo2" >立即提交</button>
    </div>
  </div>
</form>

</div>

	<table class="layui-hide" id="test" lay-filter="test"></table>

	<script type="text/html" id="toolbarDemo">
			<div class="layui-btn-container">
				<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
				<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
			<input type="button" class="layui-btn layui-btn-sm" lay-event="add" id="addtravel" value="添加景点">
			</div>
		</script>

	<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>

	<script src="../layuiadmin/layui/layui.js"></script>
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
	<script>
		layui.use('table', function() {
			var table = layui.table;

			table.render({
				elem : '#test'
				//  ,url:'/test/table/demo1.json'
				,
				url : "../../attapi/selectBypage" //模拟接口
				,
				//width:1500,
				toolbar : '#toolbarDemo',
				title : '用户数据表',
				cols : [ [ {
					type : 'checkbox',
					fixed : 'left'
				}, {
					field : 'attid',
					width : 100,
					title : '景点编号',
					sort : true
				}, {
					field : 'attname',
					title : '景点名称',
					minWidth : 100//150
				}, {
					field : 'attaddress',
					title : '景点地址',
					width :80 //400
					
				}, {
					field : 'attprice',
					//width : //150,
					
					title : '门票价格'
				}, {
					field : 'attdescribe',
					title : '景点描述',
					//width : 550
				}, {
					fixed : 'right',
					title : '操作',
					toolbar : '#barDemo',
					//width : 200
				} ]

				],
				page : true
				,id: 'testReload'
			});

			//头工具栏事件
			table.on('toolbar(test)', function(obj) {
				var checkStatus = table.checkStatus(obj.config.id);
				switch (obj.event) {
				case 'getCheckData':
					var data = checkStatus.data;
					layer.alert(JSON.stringify(data));
					break;
				case 'getCheckLength':
					var data = checkStatus.data;
					layer.msg('选中了：' + data.length + ' 个');
					break;
				case 'isAll':
					layer.msg(checkStatus.isAll ? '全选' : '未全选');
					break;
				case 'add':
					layer.open({//layui弹出层
				        type: 1,
				        area:["700px","400px"]//设置弹出层大小
				        ,content: $("#editform2"),//弹出定义的表单，表单原本是隐藏的
				        
				      });
					layui.form.render();//表单渲染，使得表单可以正常使用功能
					break;
				}
				;
			});

			//监听行工具事件
			table.on('tool(test)', function(obj) {
				var data = obj.data;
				//console.log(obj)
				if (obj.event === 'del') {
					layer.confirm('真的删除行么', function(index) {
						//obj.del();
						//layer.close(index);
						//console.log(data.attid);
						$.ajax({
							type:"DELETE",
							url:"../../attapi/deletAttrations/"+data.attid,//restful风格
							success:function(data){
								console.log(data);
								if(data.msg=="删除成功"){
									alert("删除成功");
									location.reload();
								}else{
									alert("删除失败");
									location.reload();
								}
								
							}
							
						});
						
						layer.close(index);
					});
				} else if (obj.event === 'edit') {
					$("#attid").attr("value",data.attid);
					$("#attname").attr("value",data.attname);
					$("#attaddress").attr("value",data.attaddress);
					$("#attprice").attr("value",data.attprice);
					$("#attdescribe").text(data.attdescribe);//填充文本域的值要使用text()
					layer.open({//layui弹出层
				        type: 1,
				        area:["700px","400px"]//设置弹出层大小
				        ,content: $("#editform"),//弹出定义的表单，表单原本是隐藏的
				        
				      });
					layui.form.render();//表单渲染，使得表单可以正常使用功能
					
				}
			});
			
			<!--搜索框的模糊检索功能-->
			var $ = layui.$, active = {
				    reload: function(){
				      var demoReload = $('#demoReload');
				      
				      //执行重载
				      table.reload('testReload', {
				        page: {
				          curr: 1 //重新从第 1 页开始
				        }
				        ,where: {
				         /* key: {  //key可以看成是一个对象，大括号里可以有多对键和值，
				            id: demoReload.val()//id是自己写的模糊查询方法的参数名
				          }*/
				          context: demoReload.val()
				        }
				      });
				    }
				  };
				  
				  $('.demoTable .layui-btn').on('click', function(){
				    var type = $(this).data('type');
				    active[type] ? active[type].call(this) : '';
				  });
			
				  <!--搜索框的模糊检索功能-->	
			/*提交按钮的监听事件*/
			 layui.form.on('submit(demo1)', function(data){
				//console.log(data);
				//console.log("单机");
				$.ajax({
					url:"../../attapi/updateAttrations",
					type:"PUT",
					contentType:"application/json",
					data: JSON.stringify(data.field),
					success:function(data){
						if(data.msg=="更新成功"){ 
							alert("更新成功");
							//$("#editform").css("display","none");
							layer.closeAll();//关闭所有弹出窗口
							table.reload("testReload");//刷新整个表格，testReload是layui中表格的唯一id
							$("#myeditform")[0].reset();//重新设置表单内容，清空了
							
						}else{
							alert("更新失败");
							layer.closeAll();//关闭所有弹出窗口
							table.reload("testReload");//刷新整个表格，testReload是layui中表格的唯一id
							$("#myeditform")[0].reset();//重新设置表单内容，清空了
						}
					}
					
					
					
				})
				 return false;
			});
			
			 /*提交按钮的监听事件*/
			 layui.form.on('submit(demo2)', function(data){
				//console.log(data);
				console.log(JSON.stringify(data.field));
				$.ajax({
					url:"../../attapi/insertAttrations",
					type:"POST",
					contentType:"application/json",
					data: JSON.stringify(data.field),
					success:function(data){
						if(data.msg=="添加成功"){ 
							alert("添加成功");
							//$("#editform").css("display","none");
							layer.closeAll();//关闭所有弹出窗口
							table.reload("testReload");//刷新整个表格，testReload是layui中表格的唯一id
							$("#myeditform")[0].reset();//重新设置表单内容，清空了
							
						}else{
							alert("添加失败");
							layer.closeAll();//关闭所有弹出窗口
							table.reload("testReload");//刷新整个表格，testReload是layui中表格的唯一id
							$("#myeditform")[0].reset();//重新设置表单内容，清空了
						}
					}
					
					
					
				})
				 return false;
			});
			
			
			
		});
	</script>

</body>

</html>